<template>
  <div>
    <el-table
      v-if="tableData"
      :data="
        tableData.filter(
          (data) =>
            !search || data.htitle.toLowerCase().includes(search.toLowerCase())
        )
      "
      border
      style="width: 100%"
    >
    <el-table-column type="expand">
      <template slot-scope="props">
        <el-form label-position="left" inline class="demo-table-expand">
          <el-form-item label="详细描述" style="width: 100%">
            <span>{{ props.row.hdescribe }}</span>
          </el-form-item>
        </el-form>
      </template>
    </el-table-column>
      <el-table-column prop="htitle" label="房屋信息名" width="220" sortable>
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <p>面积: {{ scope.row.harea }}</p>
            <p>楼层: {{ scope.row.hfloor }}</p>
            <p>电梯有无: {{ scope.row.helevator }}</p>
            <p>租期: {{ scope.row.hterm }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.htitle }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="hdistrict" label="区" width="80"></el-table-column>
      <el-table-column prop="haddress" label="地址" width="160"></el-table-column>
      <el-table-column prop="hprice" label="价格(元)" sortable width="100"></el-table-column>
      <el-table-column prop="hphone" label="联系电话" width="120"></el-table-column>
      <el-table-column prop="hreleasedate" label="发布日期"></el-table-column>
      <el-table-column prop="hreleasedate" label="是否精品好房">
        <template slot="header" slot-scope="{}">
          <el-input v-model="search" size="mini" placeholder="输入关键字搜索" />
        </template>
        <template slot-scope="scope">
          <select-good-house @itemchange="itemChange" :good="scope.row.goodhouse"></select-good-house>
          <el-popconfirm
            confirm-button-text="好的"
            cancel-button-text="不用了"
            icon="el-icon-info"
            icon-color="red"
            title="确定要修改吗？"
            @onConfirm="handleEdit(scope.$index, scope.row)"
          >
            <el-button slot="reference" size="mini" type="danger"
              >修改</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>

    </el-table>

    <!-- 分页 -->
    <el-row>
      <el-col :span="20" :offset="4">
        <el-pagination
          layout="prev, pager, next, jumper, total, sizes"
          background
          prev-text="prev"
          next-text="next"
          :page-size="size"
          :current-page="pageNow"
          :page-sizes="[5, 10, 15, 20]"
          @current-change="findPage"
          @size-change="findSize"
          :total="total"
        >
        </el-pagination>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { queryHouseList } from "@/network/admin";
import { updateHouse } from "@/network/admin";
import selectGoodHouse from '@/components/Select/selectGoodHouse'

export default {
  name: "houseInfo",
  components:{
    selectGoodHouse
  },
  props:{
    good:Boolean,
  },
  data() {
    return {
      tableData: [{}],
      search: "",
      show: false,
      total: 0,
      size: 5,
      pageNow: 0,
      value: '',
    };
  },
  created() {
    queryHouseList().then((res) => {
      // console.log(res);
      console.log(res.houses);
      this.tableData = res.houses;
      this.total = res.total;
    });
  },
  methods: {
    itemChange(value){
      console.log(value);
      this.value = value
    },
    // 处理
    findSize(size) {
      // console.log(size);
      this.size = size;
      this.findAllTableData((this.page - 1) * size, size);
    },

    handleEdit(index, row) {
      console.log(row);
      console.log(row.goodhouse)
      row.goodhouse = this.value;
      updateHouse(row).then((res) => {
        if (res.status) {
          this.$message({
            message: res.msg,
            type: "success",
          });
          // 刷新数据
          console.log(this.pageNow, this.size);
          this.findAllTableData(this.pageNow, this.size);
        } else {
          this.$message.error(res.msg);
        }
      });
    },

    // 处理分页的方法
    findPage(page) {
      console.log("page", page);
      this.pageNow = page;
      console.log((page - 1) * this.size);
      this.findAllTableData((page - 1) * this.size, this.size);
    },
    
    findAllTableData(page, size) {
      console.log("findAllTableData", page, size);
      page = page ? page : this.pageNow;
      size = size ? size : this.size;
      // 记住传参
      queryHouseList(page, size).then((res) => {
        console.log(res.houses);
        this.tableData = res.houses;
        this.total = res.total;
      });
    },
  },
};
</script>

<style>
.transition-box {
  margin-bottom: 10px;
  width: 100%;
  height: 500px;
  border-radius: 4px;
  padding: 40px 20px;
  box-sizing: border-box;
  margin-right: 20px;
}
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 90%;
  }
</style>
